package com.example.administrator.ttphonehelper.clock;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.administrator.ttphonehelper.R;
import com.example.administrator.ttphonehelper.base.BaseHandlerActivity;
import com.example.administrator.ttphonehelper.util.TimeUtil;
import com.example.administrator.ttphonehelper.util.ToastUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by gc on 2016/3/30.
 */
public class ClockActivity extends BaseHandlerActivity implements View.OnClickListener{
    //显示时间
    private TextView mTvTime;
    //显示日期
    private TextView mTvDate;
    //显示星期
    private TextView mTvWeek;
    //打开/关闭闹钟
    private Button mBtOffOnAlarm;
    //选择时间
    private Button mBtPickTime;
    //当前时间
    private Date mCurrTime;
    //闹钟是否开启
    private boolean mIsClockOn = false;
    //闹钟选择的时间
    private Calendar mClockCalendar;
    //警告管理类
    private AlarmManager mAlarmManager;
    //意图
    private PendingIntent mPendingIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);
        init();
    }
    private void init(){
        //初始化控件
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvDate = (TextView) findViewById(R.id.tv_date);
        mTvWeek = (TextView) findViewById(R.id.tv_week);
        mBtOffOnAlarm = (Button) findViewById(R.id.offon_alarm);
        mBtPickTime = (Button) findViewById(R.id.select_time);
        //添加点击时间
        mBtOffOnAlarm.setOnClickListener(this);
        mBtPickTime.setOnClickListener(this);
        //计时
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        },0,1000);
        //初始化时间
        mCurrTime = new Date();
        //警告管理类
        mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    }
    //处理handler发过来的消息
    @Override
    public void onHandler(Message msg) {
        switch (msg.what){
            case 0:
                //更新时间
                mCurrTime.setTime(System.currentTimeMillis());
                String time = TimeUtil.date2TimeStr(mCurrTime);
                mTvTime.setText(time);
                mTvDate.setText(TimeUtil.date2DateStr(mCurrTime));
                mTvWeek.setText(TimeUtil.date2WeekStr(mCurrTime));
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.offon_alarm:
                startAlarm();
                break;
            case R.id.select_time:
                setTime();
                break;
        }
    }
    //设置时间
    private void setTime(){
        mClockCalendar = Calendar.getInstance();
        TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                //选择的时间
                mClockCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                mClockCalendar.set(Calendar.MINUTE, minute);
                mClockCalendar.set(Calendar.SECOND, 0);
                //当前时间
                Calendar currCalendar = Calendar.getInstance();
                //选择的时间不能再当前时间之前
                if(mClockCalendar.before(currCalendar)){
                    ToastUtil.show(ClockActivity.this,"时间选择错误");
                    return;
                }
                //时间选择正确时
                mBtPickTime.setText(hourOfDay + ":" + minute);
            }
        },mClockCalendar.get(Calendar.HOUR_OF_DAY),mClockCalendar.get(Calendar.MINUTE),true);
        dialog.show();
    }
    //开关闹钟
    private void startAlarm(){
        if(mIsClockOn){

            mAlarmManager.cancel(mPendingIntent);
            mBtOffOnAlarm.setText("开启闹钟");
            mIsClockOn = false;
        }
        else{
            //判断mClockCalendar不能为空      mClockCalendar时间不能在当前时间之前
            if(mClockCalendar==null || mClockCalendar.before(Calendar.getInstance()))
            {
                ToastUtil.show(ClockActivity.this, "已经超时，请重新设置");
                return;
            }
            Intent intent = new Intent(this, AlarmActivity.class);
            mPendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
             /*
                 * 参一：闹钟的类型  AlarmManager.RTC_WAKEUP，硬件闹钟，当闹钟发躰时唤醒手机休眠；
                 * 参二：触发时间
                 * 参三：PendingIntent
                 * Intent: 意图 描述你想干的事情    立即触发
                 * PendingIntent 延迟意图   本质上就是一个Intent ，在将来的某个合适的时间点触发
                 */
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, mClockCalendar.getTimeInMillis(), mPendingIntent);

            //关闭
            mBtOffOnAlarm.setText("关闭闹钟");
            mIsClockOn = true;
        }
    }
}
